#!/bin/bash
clear
if [[ "$EUID" -ne 0 ]]; then
    echo -e "\033[1;31mScript need to be run as root!\033[0m"; exit 1
fi

apt-get -qq update
wget -q -O - https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh | bash

touch /usr/local/etc/v2ray/.trojan
touch /usr/local/etc/v2ray/.vless
touch /usr/local/etc/v2ray/.vmess

cp /etc/letsencrypt/live/cybertize.ml/fullchain.pem /usr/local/etc/v2ray/v2ray.crt
cp /etc/letsencrypt/live/cybertize.ml/privkey.pem /usr/local/etc/v2ray/v2ray.key
chmod 644 /usr/local/etc/v2ray/v2ray.crt; chmod 644 /usr/local/etc/v2ray/v2ray.key

alamat_ip=$(wget -qO- ipv4.icanhazip.com)
alamat_port=$(shuf -i 1025-9999 -n 1)
alamat_hos=$(cat /etc/environment | grep 'DOMAIN' | cut -d '=' -f 2 | head -n 1)
alamat_emel=$(cat /etc/environment | grep 'EMAIL' | cut -d '=' -f 2)
kata_laluan=$(cat /etc/environment | grep 'PASSWORD' | cut -d '=' -f 2)
user_uid="$(cat '/proc/sys/kernel/random/uuid')"
user_aid=$(shuf -i 1-254 -n 1)

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": $alamat_port,
            \"protocol\": \"trojan\",
            \"settings\": {
                \"clients\": [
                    {
                        # BEGIN doctype
                        \"password\": \"${kata_laluan}\",
                        \"email\": \"${alamat_emel}\"
                        # END doctype
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"alpn\": [
                        \"http/1.1\"
                    ],
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\"
        }
    ]
}" > /usr/local/etc/v2ray/trojan_tcp-tls.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": $alamat_port,
            \"protocol\": \"vless\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"1d716ea0-9670-4359-800b-ed8d783ec04b\",
                        \"level\": 0,
                        \"email\": \"doctype@${alamat_hos}\"
                    }
                ],
                \"decryption\": \"none\",
                \"fallbacks\": [
                    {
                        \"dest\": 8001
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\"
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        }
    ]
}" > /usr/local/etc/v2ray/vless_tcp.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": $alamat_port,
            \"protocol\": \"vless\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"1d716ea0-9670-4359-800b-ed8d783ec04b\",
                        \"level\": 0,
                        \"email\": \"doctype@${alamat_hos}\"
                    }
                ],
                \"decryption\": \"none\",
                \"fallbacks\": [
                    {
                        \"dest\": 8001
                    },
                    {
                        \"alpn\": \"h2\",
                        \"dest\": 8002
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"serverName\": \"v2ray.${alamat_hos}\",
                    \"alpn\": [
                        \"h2\",
                        \"http/1.1\"
                    ],
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        }
    ]
}" > /usr/local/etc/v2ray/vless_tcp-tls.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": $alamat_port,
            \"protocol\": \"vless\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"1d716ea0-9670-4359-800b-ed8d783ec04b\",
                        \"level\": 0,
                        \"email\": \"doctype@${alamat_hos}\"
                    }
                ],
                \"decryption\": \"none\",
                \"fallbacks\": [
                    {
                        \"dest\": 8001,
                        \"xver\": 1
                    },
                    {
                        \"alpn\": \"h2\",
                        \"dest\": 8002,
                        \"xver\": 1
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"serverName\": \"v2ray.${alamat_hos}\",
                    \"alpn\": [
                        \"h2\",
                        \"http/1.1\"
                    ],
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        }
    ]
}" > /usr/local/etc/v2ray/vless_tcp-tls-proxy.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"inbounds\": [
        {
            \"port\": $alamat_port,
            \"protocol\": \"vless\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"1d716ea0-9670-4359-800b-ed8d783ec04b\",
                        \"level\": 0,
                        \"email\": \"doctype@${alamat_hos}\"
                    }
                ],
                \"decryption\": \"none\",
                \"fallbacks\": [
                    {
                        \"dest\": 80
                    },
                    {
                        \"path\": \"/websocket\",
                        \"dest\": 1234,
                        \"xver\": 1
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"alpn\": [
                        \"http/1.1\"
                    ],
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        },
        {
            \"port\": 1234,
            \"listen\": \"127.0.0.1\",
            \"protocol\": \"vless\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"1d716ea0-9670-4359-800b-ed8d783ec04b\",
                        \"level\": 0,
                        \"email\": \"doctype@$alamat_hos\"
                    }
                ],
                \"decryption\": \"none\"
            },
            \"streamSettings\": {
                \"network\": \"ws\",
                \"security\": \"none\",
                \"wsSettings\": {
                    \"acceptProxyProtocol\": true,
                    \"path\": \"/websocket\"
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\"
        }
    ]
}" > /usr/local/etc/v2ray/vless_ws-tls.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"routing\": {
        \"domainStrategy\": \"AsIs\",
        \"rules\": [
            {
                \"type\": \"field\",
                \"ip\": [
                    \"geoip:private\"
                ],
                \"outboundTag\": \"block\"
            }
        ]
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": $alamat_port,
            \"protocol\": \"vmess\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"59a9598f-955a-437f-a5ca-ef9e8b51a26e\"
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"tcpSettings\": {
                    \"header\": {
                        \"type\": \"http\",
                        \"response\": {
                            \"version\": \"1.1\",
                            \"status\": \"200\",
                            \"reason\": \"OK\",
                            \"headers\": {
                                \"Content-Type\": [
                                    \"application/octet-stream\",
                                    \"video/mpeg\",
                                    \"application/x-msdownload\",
                                    \"text/html\",
                                    \"application/x-shockwave-flash\"
                                ],
                                \"Transfer-Encoding\": [
                                    \"chunked\"
                                ],
                                \"Connection\": [
                                    \"keep-alive\"
                                ],
                                \"Pragma\": \"no-cache\"
                            }
                        }
                    }
                },
                \"security\": \"none\"
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        },
        {
            \"protocol\": \"blackhole\",
            \"tag\": \"block\"
        }
    ]
}" > /usr/local/etc/v2ray/vmess_tcp.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"routing\": {
        \"domainStrategy\": \"AsIs\",
        \"rules\": [
            {
                \"type\": \"field\",
                \"ip\": [
                    \"geoip:private\"
                ],
                \"outboundTag\": \"block\"
            }
        ]
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": $alamat_port,
            \"protocol\": \"vmess\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"1d716ea0-9670-4359-800b-ed8d783ec04b\"
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"tcp\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        },
        {
            \"protocol\": \"blackhole\",
            \"tag\": \"block\"
        }
    ]
}" > /usr/local/etc/v2ray/vmess_tcp-tls.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"routing\": {
        \"domainStrategy\": \"AsIs\",
        \"rules\": [
            {
                \"type\": \"field\",
                \"ip\": [
                    \"geoip:private\"
                ],
                \"outboundTag\": \"block\"
            }
        ]
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": $alamat_port,
            \"protocol\": \"vmess\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"1d716ea0-9670-4359-800b-ed8d783ec04b\"
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"ws\",
                \"security\": \"none\"
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        },
        {
            \"protocol\": \"blackhole\",
            \"tag\": \"block\"
        }
    ]
}" > /usr/local/etc/v2ray/vmess_ws.json

echo "{
    \"log\": {
        \"loglevel\": \"warning\"
    },
    \"routing\": {
        \"domainStrategy\": \"AsIs\",
        \"rules\": [
            {
                \"type\": \"field\",
                \"ip\": [
                    \"geoip:private\"
                ],
                \"outboundTag\": \"block\"
            }
        ]
    },
    \"inbounds\": [
        {
            \"listen\": \"0.0.0.0\",
            \"port\": $alamat_port,
            \"protocol\": \"vmess\",
            \"settings\": {
                \"clients\": [
                    {
                        \"id\": \"1d716ea0-9670-4359-800b-ed8d783ec04b\"
                    }
                ]
            },
            \"streamSettings\": {
                \"network\": \"ws\",
                \"security\": \"tls\",
                \"tlsSettings\": {
                    \"certificates\": [
                        {
                            \"certificateFile\": \"/usr/local/etc/v2ray/v2ray.crt\",
                            \"keyFile\": \"/usr/local/etc/v2ray/v2ray.key\"
                        }
                    ]
                }
            }
        }
    ],
    \"outbounds\": [
        {
            \"protocol\": \"freedom\",
            \"tag\": \"direct\"
        },
        {
            \"protocol\": \"blackhole\",
            \"tag\": \"block\"
        }
    ]
}" > /usr/local/etc/v2ray/vmess_ws-tls.json

systemctl enable v2ray@trojan_tcp-tls; systemctl start v2ray@trojan_tcp-tls
systemctl enable v2ray@vless_tcp; systemctl start v2ray@vless_tcp
systemctl enable v2ray@vless_tcp-tls; systemctl start v2ray@vless_tcp-tls
systemctl enable v2ray@vless_tcp-tls-proxy; systemctl start v2ray@vless_tcp-tls-proxy
systemctl enable v2ray@vless_ws-tls; systemctl start v2ray@vless_ws-tls
systemctl enable v2ray@vmess_tcp; systemctl start v2ray@vmess_tcp
systemctl enable v2ray@vmess_tcp-tls; systemctl start v2ray@vmess_tcp-tls
systemctl enable v2ray@vmess_ws; systemctl start v2ray@vmess_ws
systemctl enable v2ray@vmess_ws-tls; systemctl start v2ray@vmess_ws-tls

echo
echo -e "\033[1;32mCongratulation, We are done with the v2ray installation.\033[0m"
echo
echo 'Use my referral link https://m.do.co/c/a28a40414d6a'
echo 'to gets $100 credit into your DigitalOcean account.'
echo 'Created by Doctype, Powered by Cybertize'
echo 'Copyright 2021, Allright reserved.'
echo; sleep 5
